package com.wjw.service;

import com.wjw.mapper.RoadwayTableMapper;
import com.wjw.model.Roadway_table;
import com.wjw.utils.GetSqlSessionUtils;
import com.wjw.viewmodel.Roadway_table_view;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoadwayTableService {

//    private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
//    private RoadwayTableMapper mapper=sqlsession.getMapper(RoadwayTableMapper.class);


    public List<Roadway_table_view> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadwayTableMapper mapper=sqlsession.getMapper(RoadwayTableMapper.class);
        try {
            List<Roadway_table> all = mapper.findAll();
            List<Roadway_table_view> list= new ArrayList<Roadway_table_view>();
            for(Roadway_table e:all){
                Roadway_table_view k = new Roadway_table_view();
                k.setRoad_basic_name(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                k.setRoad_type_name(mapper.findRoadTypeById(e.getRoad_type_id()).getRoad_type_name());
                k.setRoadway_area(e.getRoadway_area());
                k.setRoadway_bus(e.getRoadway_bus());
                k.setRoadway_direction(e.getRoadway_direction());
                k.setRoadway_flatstone_length(e.getRoadway_flatstone_length());
                k.setRoadway_flatstone_type(e.getRoadway_flatstone_type());
                k.setRoadway_id(e.getRoadway_id());
                k.setRoadway_lans_number(e.getRoadway_lans_number());
                k.setRoadway_left_width_range(e.getRoadway_left_width_range());
                k.setRoadway_right_width_range(e.getRoadway_right_width_range());
                k.setRoadway_sidestone_length(e.getRoadway_sidestone_length());
                k.setRoadway_sidestone_type(e.getRoadway_sidestone_type());
                k.setRoadway_thick(e.getRoadway_thick());
                k.setRoadway_type_name(mapper.findRoadwayTypeById(e.getRoadway_type_id()).getRoadway_type_name());
                k.setRoadway_thickness(e.getRoadway_thickness());
                k.setRoadway_width_range(e.getRoadway_width_range());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public List<Roadway_table_view> findByPage(int page, int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadwayTableMapper mapper=sqlsession.getMapper(RoadwayTableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Roadway_table> all = mapper.findByPage(start,limit);
            List<Roadway_table_view> list= new ArrayList<Roadway_table_view>();
            for(Roadway_table e:all){
                Roadway_table_view k = new Roadway_table_view();
                k.setRoad_basic_name(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                k.setRoad_type_name(mapper.findRoadTypeById(e.getRoad_type_id()).getRoad_type_name());
                k.setRoadway_area(e.getRoadway_area());
                k.setRoadway_bus(e.getRoadway_bus());
                k.setRoadway_direction(e.getRoadway_direction());
                k.setRoadway_flatstone_length(e.getRoadway_flatstone_length());
                k.setRoadway_flatstone_type(e.getRoadway_flatstone_type());
                k.setRoadway_id(e.getRoadway_id());
                k.setRoadway_lans_number(e.getRoadway_lans_number());
                k.setRoadway_left_width_range(e.getRoadway_left_width_range());
                k.setRoadway_right_width_range(e.getRoadway_right_width_range());
                k.setRoadway_sidestone_length(e.getRoadway_sidestone_length());
                k.setRoadway_sidestone_type(e.getRoadway_sidestone_type());
                k.setRoadway_thick(e.getRoadway_thick());
                k.setRoadway_type_name(mapper.findRoadwayTypeById(e.getRoadway_type_id()).getRoadway_type_name());
                k.setRoadway_thickness(e.getRoadway_thickness());
                k.setRoadway_width_range(e.getRoadway_width_range());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public boolean add(Roadway_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadwayTableMapper mapper=sqlsession.getMapper(RoadwayTableMapper.class);
        try {
            Roadway_table k = new Roadway_table();
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name()).getRoad_basic_id());
            k.setRoad_type_id(mapper.findRoadTypeByName(e.getRoad_type_name()).getRoad_type_id());
            k.setRoadway_area(e.getRoadway_area());
            k.setRoadway_bus(e.getRoadway_bus());
            k.setRoadway_direction(e.getRoadway_direction());
            k.setRoadway_flatstone_length(e.getRoadway_flatstone_length());
            k.setRoadway_flatstone_type(e.getRoadway_flatstone_type());
            k.setRoadway_id(e.getRoadway_id());
            k.setRoadway_lans_number(e.getRoadway_lans_number());
            k.setRoadway_left_width_range(e.getRoadway_left_width_range());
            k.setRoadway_right_width_range(e.getRoadway_right_width_range());
            k.setRoadway_sidestone_length(e.getRoadway_sidestone_length());
            k.setRoadway_sidestone_type(e.getRoadway_sidestone_type());
            k.setRoadway_thick(e.getRoadway_thick());
            k.setRoadway_type_id(mapper.findRoadwayTypeByName(e.getRoadway_type_name()).getRoadway_type_id());
            k.setRoadway_thickness(e.getRoadway_thickness());
            k.setRoadway_width_range(e.getRoadway_width_range());
            mapper.add(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean delete(int id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadwayTableMapper mapper=sqlsession.getMapper(RoadwayTableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean change(Roadway_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadwayTableMapper mapper=sqlsession.getMapper(RoadwayTableMapper.class);
        try {
            Roadway_table k = new Roadway_table();
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name()).getRoad_basic_id());
            k.setRoad_type_id(mapper.findRoadTypeByName(e.getRoad_type_name()).getRoad_type_id());
            k.setRoadway_area(e.getRoadway_area());
            k.setRoadway_bus(e.getRoadway_bus());
            k.setRoadway_direction(e.getRoadway_direction());
            k.setRoadway_flatstone_length(e.getRoadway_flatstone_length());
            k.setRoadway_flatstone_type(e.getRoadway_flatstone_type());
            k.setRoadway_id(e.getRoadway_id());
            k.setRoadway_lans_number(e.getRoadway_lans_number());
            k.setRoadway_left_width_range(e.getRoadway_left_width_range());
            k.setRoadway_right_width_range(e.getRoadway_right_width_range());
            k.setRoadway_sidestone_length(e.getRoadway_sidestone_length());
            k.setRoadway_sidestone_type(e.getRoadway_sidestone_type());
            k.setRoadway_thick(e.getRoadway_thick());
            k.setRoadway_type_id(mapper.findRoadwayTypeByName(e.getRoadway_type_name()).getRoadway_type_id());
            k.setRoadway_thickness(e.getRoadway_thickness());
            k.setRoadway_width_range(e.getRoadway_width_range());
            mapper.change(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }
}
